@extends('admin.admin')

@section('content')
@inject('pictureService', 'App\Interfaces\PictureService')
        <!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        商品管理
        <small>推荐位置</small>
        <small>推荐列表</small>
        <small>添加</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 控制面板</a></li>
        <li >商品管理</li>
        <li ><a href="/admin/recommend_position">推荐位置</a></li>
        <li ><a href="@if(@$recommend->recommend_position_id)/admin/recommend?id={{$recommend->recommend_position_id}}@endif">推荐列表</a></li>
        <li class="active">添加</li>
    </ol>
</section>

<!-- Main content -->
<section class="content">

    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title">添加推荐商品</h3>
                    <div class="pull-right">
                        <a type="button" class="btn btn-block btn-success btn-xs" onclick="javascript:history.go(-1)"><i class="icon  glyphicon glyphicon glyphicon-menu-left"></i> 返回推荐商品列表</a>
                    </div>
                </div>
                <!-- /.box-header -->
                <!-- form start -->
                {!! Form::model($recommend,['route'=>'admin.recommend.store','class'=>'form-horizontal'])  !!}
                    {!! Form::hidden('back_url',@$_SERVER['HTTP_REFERER']) !!}
                    {!! Form::hidden('recommend_position_id',@$recommend_position_id) !!}
                    <div class="box-body">

                        <div class="form-group @if($errors->has('title')) has-error   @endif">
                            <label class="col-sm-2 control-label" for="exampleInputEmail1">标题</label>
                            <div class="col-sm-6">
                            {!! Form::text('title',old('title'),array('class'=>'form-control','placeholder'=>'请填写推荐标题'))!!}
                            @if($errors->has('title'))
                                <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('title')}}</label>
                            @endif
                            </div>


                        </div>

                        <div class="form-group @if($errors->has('subtitle')) has-error   @endif">
                            <label class="col-sm-2 control-label" for="exampleInputEmail1">副标题</label>
                            <div class="col-sm-6">
                            {!! Form::text('subtitle',old('subtitle'),array('class'=>'form-control','placeholder'=>'请填写推荐副标题'))!!}
                            @if($errors->has('subtitle'))
                                <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('subtitle')}}</label>
                            @endif
                            </div>
                        </div>

                        <div class="form-group @if($errors->has('picture')) has-error   @endif">
                            <label class="col-sm-2 control-label" for="exampleInputPassword1">活动缩略图</label>

                            <div class="col-sm-2" style="width: 200px;">
                                <div class="image-uploader">
                                    <style>
                                        .upload-btn{
                                            display: block; width:48px;height:30px;position: absolute;filter:alpha(opacity=0);-moz-opacity:0;-khtml-opacity: 0;opacity: 0;
                                        }
                                    </style>
                                    <!-- /.box-header -->
                                    <div class="box-body">


                                        <div style="width: 145px;height: 145px; display: block;">
                                            <img class="image" style="width: 145px;; padding: 1px;" width="145" height="145" src="@if (old('picture',$recommend->picture)) {{$pictureService::thumb(old('picture',$recommend->picture))}} @else   http://placehold.it/145x145 @endif"/>
                                            <input type="hidden" id="image_url" name="image_url" value="{{old('image_url')}}">
                                        </div>
                                        <div class="message" style="width: 145px;height: 145px; display: block;position: absolute; padding: 5px; top:0px; color: red; text-align: center; line-height: 145px;display: none;">

                                        </div>

                                        <div class="progress progress-xxs" style="position: absolute;width: 140px; left:13px; display: none;">
                                            <div class="progress-bar progress-bar-default " role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
                                            </div>
                                        </div>
                                    </div>
                                    <!-- /.box-body -->
                                    <div class="box-footer" style="position: relative">

                                        <button class="btn btn-sm btn-default btn-flat pull-left" type="button">上传</button>
                                        {!! Form::file('file',['class'=>'upload-btn','id'=>'fileupload','type'=>'file','data-url'=>'/admin/file_upload']) !!}
                                        {!! Form::hidden('picture',old('picture'),['id'=>'thumb']) !!}

                                        <button class="btn  btn-sm btn-danger btn-flat pull-right" type="button">删除</button>
                                    </div>
                                </div>
                                <!-- /.box -->
                            </div>

                            @if($errors->has('picture'))
                                <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('picture')}}</label>
                            @endif
                        </div>

                        <div class="form-group @if($errors->has('m_picture')) has-error   @endif">
                            <label class="col-sm-2 control-label" for="exampleInputPassword1">活动缩略图</label>

                            <div class="col-sm-2" style="width: 200px;">
                                <div class="image-uploader">
                                    <style>
                                        .upload-btn{
                                            display: block; width:48px;height:30px;position: absolute;filter:alpha(opacity=0);-moz-opacity:0;-khtml-opacity: 0;opacity: 0;
                                        }
                                    </style>
                                    <!-- /.box-header -->
                                    <div class="box-body">


                                        <div style="width: 145px;height: 145px; display: block;">
                                            <img class="image" style="width: 145px;; padding: 1px;" width="145" height="145" src="@if (old('m_picture',$recommend->m_picture)) {{$pictureService::thumb(old('m_picture',$recommend->m_picture))}} @else   http://placehold.it/145x145 @endif"/>
                                            <input type="hidden" id="image_url" name="image_url" value="{{old('image_url')}}">
                                        </div>
                                        <div class="message" style="width: 145px;height: 145px; display: block;position: absolute; padding: 5px; top:0px; color: red; text-align: center; line-height: 145px;display: none;">

                                        </div>

                                        <div class="progress progress-xxs" style="position: absolute;width: 140px; left:13px; display: none;">
                                            <div class="progress-bar progress-bar-default " role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
                                            </div>
                                        </div>
                                    </div>
                                    <!-- /.box-body -->
                                    <div class="box-footer" style="position: relative">

                                        <button class="btn btn-sm btn-default btn-flat pull-left" type="button">上传</button>
                                        {!! Form::file('file',['class'=>'upload-btn','id'=>'fileupload','type'=>'file','data-url'=>'/admin/file_upload']) !!}
                                        {!! Form::hidden('m_picture',old('m_picture'),['id'=>'thumb']) !!}

                                        <button class="btn  btn-sm btn-danger btn-flat pull-right" type="button">删除</button>
                                    </div>
                                </div>
                                <!-- /.box -->
                            </div>

                            @if($errors->has('m_picture'))
                                <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('m_picture')}}</label>
                            @endif
                        </div>

                        <div class="form-group @if($errors->has('description')) has-error   @endif">
                            <label class="col-sm-2 control-label" for="exampleInputEmail1">描述</label>
                            <div class="col-sm-6">
                            {!! Form::textarea('description',old('description'),array('class'=>'form-control','placeholder'=>'描述'))!!}
                            @if($errors->has('description'))
                                <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('description')}}</label>
                            @endif
                            </div>
                        </div> 

                        <div class="form-group @if($errors->has('link')) has-error   @endif">
                            <label class="col-sm-2 control-label" for="exampleInputEmail1">链接商品ID</label>
                            <div class="col-sm-6">
                            {!! Form::text('link',old('link'),array('class'=>'form-control','placeholder'=>'链接商品ID'))!!}
                            @if($errors->has('link'))
                                <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i> {{$errors->first('link')}}</label>
                            @endif
                            </div>

                        </div>                    
                        
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer">
                        <div class="form-group no-padding">
                            <label class="col-sm-2 control-label">&nbsp;</label>
                            <div class="col-sm-3">
                                <button type="submit" class="btn btn-primary">保存</button>
                            </div>
                        </div>
                    </div>
                {!! Form::close()  !!}
            </div>
            <!-- /.box -->
        </div>
    </div>



</section>
<!-- /.content -->

<script src="{{ asset ("/components/blueimp-file-upload/js/vendor/jquery.ui.widget.js") }}"></script>
<script src="{{ asset ("/components/blueimp-file-upload/js/jquery.iframe-transport.js") }}"></script>
<script src="{{ asset ("/components/blueimp-file-upload/js/jquery.fileupload.js") }}"></script>
<script>
    $(function () {
        $('.image-uploader').each(function(k,imageUploader){
            var wrapper = $(imageUploader);//盒子
            var uploadBtn = wrapper.find('#fileupload');
            var imageFile = wrapper.find('.image');
            var progress = wrapper.find('.progress');
            var progressBar = progress.find('.progress-bar');
            var inputFile = wrapper.find('#thumb');
            var imageUrl = wrapper.find('#image_url');
            var message = wrapper.find('.message');

            uploadBtn.click(function(){
                progressBar.css('width', 0 + '%');
                imageFile.attr('src','http://placehold.it/145x145');
                inputFile.val('');
                message.hide();

            });
            wrapper.find('.btn-danger').click(function(){
                progressBar.css('width', 0 + '%');
                imageFile.attr('src','http://placehold.it/145x145');
                inputFile.val('');
                message.hide();

            });

            uploadBtn.fileupload({
                dataType: 'json',
                headers:{'X-CSRF-TOKEN':'{{csrf_token()}}'},
                add: function (e, data) {
                    data.submit();
                },
                progressall: function (e, data) {
                    progress.fadeIn();
                    var percent = parseInt(data.loaded / data.total * 100, 10);
                    if (progress==1){
                        progress.show()
                    }
                    progressBar.css('width', percent + '%');
                    setTimeout(function () {
                        progress.fadeOut();
                    }, 500);
                },
                done: function (e, data) {
                    imageFile.attr('src',data.result.url);
                    inputFile.val(data.result.id);
                    imageUrl.val(data.result.url);
                },
                complete:function(){
                }
            });
        });
    });
</script>
@endsection